﻿$(document).ready(function () {
    iniciarTabsFormulario();
    sobreescreveAlert();
    $(".qsbox").attr('onkeyup', '');
});


function sobreescreveAlert() {
    if ($("#alert").length > 0) {
        window.alert = function (message) {
            $(".alert-text").text(message);
            $("#alert").modal('toggle');
        }
    }
}

function soNumeros(obj, substring) {
    obj.val(obj.val().replace(/\D/g, ""));
    if (obj.val().length > substring)
        obj.val(obj.val().substring(0, substring));
}



function soNumerosData(obj, substring) {
    obj.val(obj.val().replace(/\D/g, ""));
    if (obj.val() > 31)
        obj.val("31");
    if (obj.val().length > substring)
        obj.val(obj.val().substring(0, substring));
}


function soNumerosDecimais(obj, substring) {
    obj.val(obj.val().replace(',', '.').replace(/[^0-9.0-9\s]/, ""));
    if (obj.val().length > substring)
        obj.val(obj.val().substring(0, substring));
}

function insereGrid(obj, urlAjax, colunas, buttons, search, title, sortname, sortorder, usepager, width, height) {
    $("#" + obj).flexigrid({
        url: urlAjax,
        dataType: 'json',
        colModel: colunas,
        buttons: buttons,
        searchitems: search,
        sortname: sortname,
        sortorder: sortorder,
        usepager: usepager,
        title: title,
        useRp: true,
        rp: 15,
        showTableToggleBtn: true,
        //width: width,
        height: height
    });
}

function iniciarTabsFormulario() {
    if ($("#tabCadastro").length == 0)
        return;

    $("#tabCadastro a").click(function (e) {
        e.preventDefault();
        $(this).tab("show");
    });

    $("#tabCadastro a:first").tab("show");
}

function replaceAll(str, find, replace) {
    var newstr = "";
    var fl = find.length;

    for (var i = 0; i < str.length; i++) {
        comparison = fl > 1 ? str.substring(i, i + fl) : str[i];

        if (comparison == find) {
            newstr = newstr + replace;
            i += fl - 1;
        } else {
            newstr = newstr + str[i];
        }
    }

    return newstr;
}

function verificaVazio(str) {
    //str = str.replace(" ", "");
    //if (str.indexOf("||") > -1 || str.indexOf("|#") > -1 || str.indexOf("#|") > -1)
    //    return false;
    //else
        return true;
}



function automaticoCalculo() {
    if ($("#duracao").val() != "" && $("#valor_parcela").val() != "") {
        $("#valor_total").val($("#duracao").val() * $("#valor_parcela").val());
    }
}